package com.service.site.utils;


import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class JsonConverter {
	
	public static String toJson(Map<String, Object> data) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			String jsonString = mapper.writeValueAsString(data);
			return jsonString;
		} catch (Exception e) {
			throw new RuntimeException("Map creation error!", e);
		}
	}

	public static String toJson(Object obj){
		ObjectMapper mapper = new ObjectMapper();
		StringWriter writer = new StringWriter();
		try {
			mapper.writeValue(writer, obj);
		} catch (Exception e) {
			throw new RuntimeException("JSON creation error!", e);
		}
		return writer.toString();
	}

	public static Map<String, Object> toMap(String jsonString) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> result = null;
		try {
			result = mapper.readValue(jsonString, Map.class);
		} catch (Exception e) {
			throw new RuntimeException("Map creation error!", e);
		}
		return result;
	}
	
	
	public static HashMap toJsonObject(Object obj){
		ObjectMapper mapper = new ObjectMapper();
		return mapper.convertValue(obj, HashMap.class);
	}

}